/*
 * StringTemplateV4RenderedResource.java
 *
 * Copyright 2013 Jack Leow
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package my.edu.clhs.springframework.core.io;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.core.io.Resource;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.compiler.CompiledST;
import org.stringtemplate.v4.compiler.Compiler;

/**
 * The result of applying a StringTemplate 4 <tt>template</tt> to a
 * <tt>model</tt> as a {@link Resource}.
 *
 * @author Jack Leow
 * @since January 2013
 */
public class StringTemplateV4RenderedResource
        extends AbstractTemplateRenderedResource<CompiledST> {
    public StringTemplateV4RenderedResource(
            Resource template, Map<String,?> model) {
        super(template, model);
    }
    
    public StringTemplateV4RenderedResource(
            Resource template, Charset charset, Map<String,?> model) {
        super(template, charset, model);
    }
    
    private static final STGroup stGroup = new STGroup('$', '$');
    
    @Override
    protected CompiledST createTemplate(
            InputStream templateResourceInputStream, Charset charset)
            throws IOException {
        final BufferedReader tmplReader = new BufferedReader(
            new InputStreamReader(templateResourceInputStream, charset)
        );
        final StringBuffer tmplBuf = new StringBuffer();
        String tmplLine = tmplReader.readLine();
        while (tmplLine != null) {
            tmplBuf.append(tmplLine).append('\n');
            tmplLine = tmplReader.readLine();
        }
        
        CompiledST template = new Compiler(stGroup).compile(tmplBuf.toString());
        template.hasFormalArgs = false;
        template.name = ST.UNKNOWN_NAME;
        template.defineImplicitlyDefinedTemplates(stGroup);
        
        return template;
    }

    @Override
    protected InputStream applyTemplate(
            CompiledST template, Charset charset, Map<String, ?> model)
            throws IOException {
        ST st = stGroup.createStringTemplate(template);
        
        for (Entry<String, ?> entry : model.entrySet()) {
            st.add(entry.getKey(), entry.getValue());
        }
        
        String content = st.render();
        
        return new ByteArrayInputStream(content.getBytes(charset));
    }
}
